﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Book
{
    public partial class AddInfo : Form
    {
        public AddInfo()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 确认添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Ok_Click(object sender, EventArgs e)
        {
            string title = txt_Title.Text;
            
            string author = txt_Author.Text;
            
            string time = txt_Time.Text;

            var nullTitle = string.IsNullOrEmpty(title);
            var nullAuthor = string.IsNullOrEmpty(author);
            if (!nullTitle && !nullAuthor)
            {
                string sql = string.Format("insert into Articles(Title,Author,CreatedTime) values('{0}','{1}','{2}')", title, author, time);
                var res = DbHelper.GetAddOrDelOrUp(sql);
                //显示信息
                if (res >=1)
                {
                    MessageBox.Show("增加成功！", "消息提示");
                    BookInfo book = new BookInfo();
                    book.Show();
                    this.Close();
                }
            }
            else if (nullTitle && nullAuthor)
            {
                MessageBox.Show("书名和作者不能为空！", "消息提示");
            }
            else if (nullTitle)
            {
                MessageBox.Show("书名不能为空！", "消息提示");
            }
            else if (nullAuthor)
            {
                MessageBox.Show("作者不能为空！", "消息提示");
            }
            else
            {
                MessageBox.Show("增加失败！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Close_Click(object sender, EventArgs e)
        {
            this.Close();
            BookInfo book = new BookInfo();
            book.Show();
        }
    }
}
